from django.conf import settings
from django.core.files.storage import Storage
from fdfs_client.client import Fdfs_client


class FastDFSStorage(Storage):
    """自定义文件存储系统，修改存储的方案"""

    def __init__(self, fdfs_base_url=None):
        """
        构造方法，可以不带参数，也可以携带参数
        :param base_url: Storage的IP
        """
        self.fdfs_base_url = fdfs_base_url or settings.FDFS_BASE_URL

    def _open(self, name, mode='rb'):
        pass

    def _save(self, name, content):
        pass

    def url(self, name):
        """
        返回name所指文件的绝对URL
        :param name: 要读取文件的引
        用:group1/M00/00/00/wKhnnlxw_gmAcoWmAAEXU5wmjPs35.jpeg
        :return:
        http://192.168.176.134:8888/group1/M00/00/00/wKhnnlxw_gmAcoWmAAEXU5wmjPs35.jpeg
        """
        # return '192.168.176.134:8888/' + name
        # return 'http://image.meiduo.site:8888/' + name
        return self.fdfs_base_url + name


if __name__ == '__main__':
    client = Fdfs_client('client.conf')
    ret = client.upload_by_filename('/mnt/hgfs/ubantu18/sixstar/图片/可爱萝莉.jpg')
    print(ret)
    ret = client.upload_by_filename('/mnt/hgfs/ubantu18/sixstar/图片/姐妹花2.jpg')
    print(ret)
    ret = client.upload_by_filename('/mnt/hgfs/ubantu18/sixstar/图片/性感高冷.jpg')
    print(ret)
# "192.168.176.135:8888/group1/M00/00/00/wKiwh2EM5WqANN6ZAAO2k01LJrU863.jpg"
"192.168.142.129:8888/group1/M00/00/00/wKiOgWEVsHaAFQ8PAAGchXWt-7A465.jpg"
